package com.caifu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caifu.mapper.YlCostMapper;
import com.caifu.pojo.*;
import com.caifu.service.ICostService;
import com.caifu.util.Constance;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class CostServiceImpl implements ICostService {

    @Resource
    private YlCostMapper costMapper;

    @Override
    public JSONObject saveCostInfo(YlCost cost, SysUser user) throws Exception {
        JSONObject result = new JSONObject();
        if(null==user){
            result.put("code","1001");
            result.put("codeDesc","登录超时，请重新登录");
            return result;
        }
        if(null==cost){
            result.put("code","1002");
            result.put("codeDesc","要保存的内容为空");
            return result;
        }
        try {
            //验证内容是否已存在
            String costType = Constance.Null2EmptyTrim(cost.getCostType());
            String costName = Constance.Null2EmptyTrim(cost.getCostName());
            String pensionNo = Constance.Null2EmptyTrim(user.getCurrentPensionNo());
            if("104".equals(costType)){
                //如果是伙食费
                YlCost oldCost = costMapper.queryCostInfo(null,costType,pensionNo);
                //如果伙食费已存在则更新伙食费信息，否则创建伙食费信息
                if(null!=oldCost){
                    oldCost.setCostPrice(cost.getCostPrice());
                    costMapper.updateById(oldCost);
                }else {
                    cost.setCostName("伙食费");
                    cost.setPensionNo(pensionNo);
                    cost.setTellerNo(user.getUserNo()+"");
                    cost.setCreateTime(Constance.currentDateTime());
                    cost.setStatus("1");
                    costMapper.insert(cost);
                }
            }else {
                //验证名称是否已经存在
                YlCost oldCost = costMapper.queryCostInfo(costName,costType,pensionNo);
                if(null!=oldCost){
                    result.put("code","1003");
                    result.put("codeDesc","该名称已存在");
                    return result;
                }
                cost.setPensionNo(pensionNo);
                cost.setTellerNo(user.getUserNo()+"");
                cost.setCreateTime(Constance.currentDateTime());
                cost.setStatus("1");
                costMapper.insert(cost);
            }
            result.put("code","0000");
            result.put("codeDesc","添加成功");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code","9999");
            result.put("codeDesc","添加失败");
            return result;
        }
    }

    @Override
    public JSONObject queryCostInfoList(String costType, SysUser user) throws Exception {
        JSONObject result = new JSONObject();
        JSONArray dataArray = new JSONArray();
        int costCount = 0;
        if(null==user){
            result.put("code","0000");
            result.put("costCount",costCount);
            result.put("codeDesc","登录超时，请重新登录");
            return result;
        }
        try {
            QueryWrapper<YlCost> costQueryWrapper  = new QueryWrapper<>();
            costQueryWrapper.eq("COST_TYPE",costType);
            costQueryWrapper.eq("PENSION_NO",user.getCurrentPensionNo());
            costQueryWrapper.eq("STATUS","1");
            costQueryWrapper.orderByAsc("CREATE_TIME");
            List<YlCost> costList = costMapper.selectList(costQueryWrapper);
            if(null!=costList&&costList.size()>0){
                costCount =costList.size();
                for (int i=0;i<costCount;i++) {
                    YlCost c = costList.get(i);
                    if(null!=c){
                        JSONObject data = new JSONObject();
                        data.put("costNo",c.getCostNo());
                        data.put("costName",c.getCostName());
                        data.put("costPrice",c.getCostPrice());
                        data.put("order",i+1);
                        dataArray.add(data);
                    }
                }
            }
            result.put("code","0000");
            result.put("costCount",costCount);
            result.put("data",dataArray);
            result.put("codeDesc","查询成功");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code","0000");
            result.put("costCount",0);
            result.put("codeDesc","查询失败");
            return result;
        }
    }

    @Override
    public JSONObject queryMealFeeInfo(SysUser user) throws Exception {
        BigDecimal mealPrice = new BigDecimal(0.0);
        JSONObject result = new JSONObject();
        try {
            YlCost mealCost = costMapper.queryCostInfo(null,"104",user.getCurrentPensionNo());
            if(null!=mealCost){
                mealPrice = mealCost.getCostPrice();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.put("code","0000");
        result.put("mealPrice",mealPrice);
        result.put("codeDesc","查询成功");
        return result;
    }

    @Override
    public JSONObject queryFeeInfoDetail(String costNo) throws Exception {
        JSONObject result = new JSONObject();
        if(StringUtils.isBlank(costNo)){
            result.put("code","1001");
            result.put("codeDesc","查询失败！");
            return result;
        }
        try {
            YlCost costInfo = costMapper.selectById(costNo);
            if(null==costInfo){
                result.put("code","1001");
                result.put("codeDesc","查询失败！");
                return result;
            }
            result.put("costName",costInfo.getCostName());
            result.put("costNo",costInfo.getCostNo());
            result.put("costType",costInfo.getCostType());
            result.put("costImg",costInfo.getCostImg());
            result.put("costPrice",costInfo.getCostPrice());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code","1001");
            result.put("codeDesc","查询失败！");
            return result;
        }
    }

    @Override
    public JSONObject updateFeeInfo(YlCost costInfo) throws Exception {
        JSONObject result = new JSONObject();
        if(null==costInfo){
            result.put("code","1001");
            result.put("codeDesc","信息为空");
            return result;
        }
        try {
            //查询原楼层信息
            YlCost oldCost = costMapper.selectById(costInfo.getCostNo());
            if(null==oldCost){
                result.put("code","1002");
                result.put("codeDesc","原信息不存在！");
                return result;
            }
            String pensionNo = Constance.Null2EmptyTrim(oldCost.getPensionNo());
            String costType = Constance.Null2EmptyTrim(oldCost.getCostType());
            String oldCostName = Constance.Null2EmptyTrim(oldCost.getCostName());
            String newCostName = Constance.Null2EmptyTrim(costInfo.getCostName());
            if(!oldCostName.equals(newCostName)){
                //验证楼层编号是否重复
                YlCost exitCost = costMapper
                        .queryCostInfo(newCostName,costType,pensionNo);
                if(null!=exitCost){
                    result.put("code","1003");
                    result.put("codeDesc","该名称已存在！");
                    return result;
                }
            }
            oldCost.setCostName(costInfo.getCostName());
            oldCost.setCostPrice(costInfo.getCostPrice());
            String costImg = Constance.Null2EmptyTrim(costInfo.getCostImg());
            if(!"".equals(costImg)){
                oldCost.setCostImg(costImg);
            }
            costMapper.updateById(oldCost);
            result.put("costType",oldCost.getCostType());
            result.put("code","0000");
            result.put("codeDesc","修改成功！");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code","9999");
            result.put("codeDesc","修改失败！");
            return result;
        }
    }

    @Override
    public JSONObject deleteFeeInfo(String costNo) throws Exception {
        JSONObject result = new JSONObject();
        if(StringUtils.isBlank(costNo)){
            result.put("code","1001");
            result.put("codeDesc","删除失败！");
            return result;
        }
        try {
            YlCost costInfo = costMapper.selectById(costNo);
            if(null==costInfo){
                result.put("code","1001");
                result.put("codeDesc","删除失败！");
                return result;
            }
            costInfo.setStatus("9");
            costMapper.updateById(costInfo);
            result.put("costType",costInfo.getCostType());
            result.put("code","0000");
            result.put("codeDesc","删除成功！");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code","9999");
            result.put("codeDesc","删除失败！");
            return result;
        }
    }
}
